﻿using System.Net.Http;
using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Xc.DataAccess.Core;
using Xc.DataAccess.Protocoals.WaterMeters.Models;
using Xc.DataAccess.Protocoals.WaterMeters.Models.Requests;

namespace Xc.DataAccess.WaterMeters.ClientSides.Services
{
    /// <summary>
    /// 设备客户端服务
    /// </summary>
    public class WaterDeviceSendService : IDevice, IWaterDeviceSendService
    {
        /// <summary>
        /// 服务器地址
        /// </summary>
        public string ServerPath { get; set; }
        /// <summary>
        /// Http客户端工厂
        /// </summary>
        private readonly IHttpClientFactory httpContextFactory;
        /// <summary>
        /// 设备客户端服务
        /// </summary>
        /// <param name="httpContextFactory"></param>
        public WaterDeviceSendService(IHttpClientFactory httpContextFactory)
        {
            this.httpContextFactory = httpContextFactory;
        }

        /// <summary>
        /// 创建客户端
        /// </summary>
        /// <returns></returns>
        private HttpClient CreatClient()
        {
            var client = httpContextFactory.CreateClient();
            client.BaseAddress = new System.Uri(ServerPath);
            return client;
        }
        /// <summary>
        /// 获取表计最新的地址
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public async Task<Result> GetMeterAddress(string deviceId)
        {
            using (var client = CreatClient())
            {
                return await client.GetFromJsonAsync<Result>($"api/device/GetMeterAddress?deviceId={deviceId}");
            }
        }
        /// <summary>
        /// 获取表计数据
        /// </summary>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public async Task<Result> GetMeterData(string deviceId)
        {
            using (var client = CreatClient())
            {
                return await client.GetFromJsonAsync<Result>($"api/device/GetMeterData?deviceId={deviceId}");
            }
        }
        /// <summary>
        /// 获取在线列表
        /// </summary>
        /// <param name="devcieidLike"></param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="showInvalidateDevcie"></param>
        /// <returns></returns>
        public async Task<ResultData<OnlineDevice>> Onlines(string devcieidLike = null, int skip = 0, int take = 1000, ShowInvalidateDevcie showInvalidateDevcie = ShowInvalidateDevcie.Hide)
        {
            using (var client = CreatClient())
            {
                return await client.GetFromJsonAsync<ResultData<OnlineDevice>>($"api/device/onlines?devcieidLike={devcieidLike}&skip={skip}&take={take}&showInvalidateDevcie={showInvalidateDevcie}");
            }
        }
        /// <summary>
        /// 设置囤积量报警量
        /// </summary>
        /// <param name="setAlarmVolumeRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetAlarmVolume(SetAlarmVolumeRequest setAlarmVolumeRequest)
        {
            return await RequestCore(setAlarmVolumeRequest);
        }

        /// <summary>
        /// 关闭阀门
        /// </summary>
        /// <param name="setCloseValveRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetCloseValve(SetCloseValveRequest setCloseValveRequest)
        {
            return await RequestCore(setCloseValveRequest);
        }
        /// <summary>
        /// 发起请求
        /// </summary>
        /// <typeparam name="TSend"></typeparam>
        /// <param name="send"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<Result> RequestCore<TSend>(TSend send, [CallerMemberName] string name = null)
        {
            try
            {
                using (var client = CreatClient())
                {
                    var respond = await client.PostAsJsonAsync($"api/device/{name}", send);
                    if (!respond.IsSuccessStatusCode)
                    {
                        return Result.Reject(await respond.Content.ReadAsStringAsync());
                    }
                    else
                    {
                        return await respond.Content.ReadFromJsonAsync<Result>();
                    }
                }
            }
            catch (System.Exception err)
            {
                return Result.Reject(err.Message);
            }

        }
        /// <summary>
        /// 设置表计IP地址
        /// </summary>
        /// <param name="setMeterIpAddressRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetMeterIpAddress(SetMeterIpAddressRequest setMeterIpAddressRequest)
        {
            return await RequestCore(setMeterIpAddressRequest);
        }
        /// <summary>
        /// 多次卡充值
        /// </summary>
        /// <param name="setMeterMutipleChargeRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetMeterMutipleCharge(SetMeterMutipleChargeRequest setMeterMutipleChargeRequest)
        {
            return await RequestCore(setMeterMutipleChargeRequest);
        }
        /// <summary>
        /// 修改上报周期和超时限制
        /// </summary>
        /// <param name="setMeterUploadIntervalRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetMeterUploadInterval(SetMeterUploadIntervalRequest setMeterUploadIntervalRequest)
        {
            return await RequestCore(setMeterUploadIntervalRequest);
        }
        /// <summary>
        /// 关阀
        /// </summary>
        /// <param name="setMeterValueRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetMeterValue(SetMeterValueRequest setMeterValueRequest)
        {
            return await RequestCore(setMeterValueRequest);
        }
        /// <summary>
        /// /开阀
        /// </summary>
        /// <param name="setOpenValveRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetOpenValve(SetOpenValveRequest setOpenValveRequest)
        {
            return await RequestCore(setOpenValveRequest);
        }
        /// <summary>
        /// 恢复出厂设置
        /// </summary>
        /// <param name="deviceid"></param>
        /// <returns></returns>
        public async Task<Result> SetRestoreFactory(string deviceid)
        {
            using (var client = CreatClient())
            {
                return await client.GetFromJsonAsync<Result>($"api/device/SetRestoreFactory?deviceId={deviceid}");
            }
        }
        /// <summary>
        /// 用户充值指令
        /// </summary>
        /// <param name="setUserChangeRequest"></param>
        /// <returns></returns>
        public async Task<Result> SetUserChange(SetUserChangeRequest setUserChangeRequest)
        {
            return await RequestCore(setUserChangeRequest);
        }
    }
}
